package org.mcflier.appuntamento.io.driver.imdb;

import java.io.IOException;
import java.util.List;
import java.util.Properties;

import org.mcflier.appuntamento.core.exception.PropertiesException;
import org.mcflier.appuntamento.core.properties.PropertiesManager;
import org.mcflier.appuntamento.data.model.Movie;
import org.mcflier.appuntamento.i18n.Country;
import org.mcflier.appuntamento.io.driver.imdb.interfaces.IIMDBDriver;
import org.mcflier.appuntamento.io.exception.MovieNotFoundException;
import org.mcflier.appuntamento.io.exception.ParsingException;
import org.mcflier.appuntamento.io.exception.RemoteConnectionException;
import org.mcflier.appuntamento.io.exception.RemoteResourceNotFoundException;

//AbstractDriver implementation wrapper
public class IMDBDriver implements IIMDBDriver {
	
	private static IMDBDriver instance;
	
	static{
		instance = null;
	}
	
	public static IMDBDriver getInstance() throws IOException, InstantiationException, IllegalAccessException, ClassNotFoundException, PropertiesException{
		if(instance == null)
			instance = new IMDBDriver();
		
		return instance;
	}
	
	private Properties properties;
	private AbstractIMDBDriver driver;
	
	private IMDBDriver() throws InstantiationException, IllegalAccessException, ClassNotFoundException, PropertiesException{
		PropertiesManager propertiesManager = PropertiesManager.getInstance();
		this.properties = propertiesManager.getProperties();
		
		this.driver = (AbstractIMDBDriver)Class.forName(this.properties.getProperty(DRIVER_CLASSPATH)).newInstance();
		this.driver.init(properties);
	}

	@Override
	public Movie getMovie(String id) throws ParsingException, MovieNotFoundException, RemoteConnectionException {
		return this.driver.getMovie(id);
	}

	@Override
	public List<Movie> getMovies(List<String> ids) throws ParsingException, RemoteConnectionException {
		return this.driver.getMovies(ids);
	}

	@Override
	public List<Movie> getShowtimes(Country country, String zipCode) throws PropertiesException, ParsingException, RemoteResourceNotFoundException, RemoteConnectionException {
		return this.driver.getShowtimes(country, zipCode);
	}

	@Override
	public List<Movie> getNowPlaying() throws ParsingException, RemoteConnectionException, RemoteResourceNotFoundException {
		return this.driver.getNowPlaying();
	}

	@Override
	public List<Movie> getNowPlaying(int page) throws ParsingException, RemoteConnectionException, RemoteResourceNotFoundException {
		return this.driver.getNowPlaying(page);
	}
}